﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.ComponentModel;

namespace Devmil.MVVM.Sample.ViewModel
{
    public class MainViewModelTraditional : INotifyPropertyChanged
    {
        private ContactViewModelTraditional _CurrentContact;

        public ContactViewModelTraditional CurrentContact
        {
            get
            {
                return _CurrentContact;
            }
            set
            {
                bool changed = _CurrentContact != value;
                if (changed)
                {
                    if (_CurrentContact != null)
                        _CurrentContact.PropertyChanged -= CurrentContact_PropertyChanged;
                    _CurrentContact = value;
                    if (_CurrentContact != null)
                        _CurrentContact.PropertyChanged += CurrentContact_PropertyChanged;
                    OnPropertyChanged("CurrentContact");
                    OnPropertyChanged("ContactNameString");
                }
            }
        }

        void CurrentContact_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Name")
                OnPropertyChanged("ContactNameString");
        }

        public string CurrentContactNameString
        {
            get
            {
                if (CurrentContact != null)
                    return CurrentContact.Name;
                return "No Contact selected";
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
